<?php
namespace Home\Controller\Goods;
use Home\Controller\Auto\AutoController;
class OrderController extends AutoController {
	public function index(){
		if(empty(session('cart_id'))){
			echo "<script>alert('您没有可结算的商品,请先选择购物车里的商品');location.href='/home/goods/cart/index'</script>";
		}
		$data = self::order_data();
		// var_dump($data);die;
		$this->xiang=self::xiangguan($data[0]['pinpai']);
		$this->list=$data;
		$this->addr= M('g_address')->where('uid='.session('home_id'))->select();
		$this->display();
	}
	public static function order_data(){
		foreach(session('cart_id') as $v){
			$data[] = M('g_cart')->alias('a')
					->field(array('a.*,b.goods_name,b.goods_price,b.goods_stock,b.pinpai'))
					->join('left join think_g_goods b on a.gid=b.id')
					->where('a.id='.$v)
					->find();
		}
		$pic = M('g_goods_pic')->select();
		foreach($data as &$v){
			foreach($pic as $val){
				if($v['gid']==$val['gid'] && $val['goods_zpic']=='1'){
					$v['pic']=$val['goods_pic'];
					$v['subtotal']=$v['num']*$v['goods_price'];
				}
			}
		}
		return $data;
	}
	public static function xiangguan($pinpai){
		$pic = M('g_goods_pic')->select();
		$xiangguan = M('g_goods')->where('pinpai='.$pinpai)->select();
		foreach($xiangguan as $k2=>&$v2){
			foreach($pic as $k1=>$v1){
				if($v1['gid']==$v2['id'] && $v1['goods_zpic']==1){
					$v2['zpic']=$v1['goods_pic'];
				}
			}
		}
		$x = [];
		if(count($xiangguan)>=3){
			$xiabiao = array_rand($xiangguan,'3');
			foreach($xiabiao as $k3=>$v3){
				$x[]=$xiangguan[$v3];
			}
		}else{
			$x = $xiangguan;
		}
		return $x;
	}
	public function insert(){
		// var_dump(I('post.'));
		$data['gid']=json_encode(I('post.arr'));
		$data['aid']=I('post.aid');
		$data['total']=I('post.total');
		$data['bianhao']=time().rand(1000000,1000000000);
		$data['time']=time();
		$data['uid']=session('home_id');
		$data['status']=0;
		// var_dump($data);die;
		$order = M('g_order');
		$order->create($data);
		if($order->add()){
			foreach(I('post.arr') as $v){
				M('g_cart')->where('id='.$v)->save(['status'=>'1']);
			}
			session('cart_id',null);
			echo "yes";
			return ;
		}else{
			echo 'no';
			return ;
		}
	}
	public static function show_data(){
			$data = M('g_cart')->alias('a')
					->field(array('a.*,b.goods_name,b.goods_price,b.goods_stock,b.pinpai'))
					->join('left join think_g_goods b on a.gid=b.id')
					->where('a.uid='.session('home_id'))
					->select();
		$pic = M('g_goods_pic')->select();
		foreach($data as &$v){
			foreach($pic as $val){
				if($v['gid']==$val['gid'] && $val['goods_zpic']=='1'){
					$v['pic']=$val['goods_pic'];
					$v['subtotal']=$v['num']*$v['goods_price'];
				}
			}
		}
		return $data;
	}
	public function show(){
		$order = M('g_order');
		$where['a.uid']=session('home_id');
		$where['a.status']=['lt','5'];
		$list = $order->alias('a')
			->field(array('a.*,b.zhuzhi,b.name,b.phone'))
			->join('left join think_g_address b on a.aid=b.id')
			->where($where)
			->select();
		$cart = M('g_cart')->select();
		$pic = M('g_goods_pic')->select();
		$cart_arr=[];
		foreach($list as &$list_v){
			$gid=json_decode($list_v['gid']);
			// var_dump($gid);
			foreach($gid as $key=>$gid_v){
				$gid[$key] = M('g_cart')->where('id='.$gid_v)->find();
				$goods = M('g_goods')->where('id='.$gid[$key]['gid'])->find();
				foreach($pic as $pic_v){
					if($pic_v['gid']==$goods['id'] && $pic_v['goods_zpic']==1){
						$goods['pic']=$pic_v['goods_pic'];
					}
				}
				$gid[$key]['goods']=$goods;
			}
			$list_v['cart']=$gid;
		}

		// echo '<pre>';
		// print_r($list);
		// var_dump(null);die;
		$this->orders = $list;
		$this->display();
	}
	public function del(){
		if(M('g_order')->where('id='.I('post.id'))->save(['status'=>5])){
			echo "yes";
		}else{
			echo 'no';
		}
	}
	public function status(){
		// echo 'yes';
		if(M('g_order')->where('id='.I('post.id'))->save(['status'=>2])){
			echo 'yes';
			return ;
		}else{
			echo 'no';
			return ;
		}
	}
}